package cn.ctbu.xiaopeng.studentscoremanage;

import java.util.*;

public class MockDataGenerator {

    // 随机生成学生姓名
    private static String[] firstNames = {"张", "李", "王", "刘", "陈", "杨", "赵", "黄", "周", "吴"};
    private static String[] lastNames = {"伟", "娜", "婷", "磊", "超", "军", "洋", "鹏", "亮", "俊"};

    // 随机生成课程名称
    private static String[] courseNames = {
            "数据结构", "算法设计", "计算机网络", "操作系统", "数据库原理",
            "离散数学", "编译原理", "人工智能", "软件工程", "计算机组成原理"
    };

    // 随机生成班级编号
    private static String[] classNames = {
            "计算机科学1班", "计算机科学2班", "电子工程1班", "电子工程2班",
            "法学1班", "法学2班", "数学1班", "数学2班"
    };

    // 随机生成教师姓名
    private static String[] teacherFirstNames = {"赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈"};
    private static String[] teacherLastNames = {"亮", "平", "军", "静", "娜", "涛", "刚", "伟", "琳", "霞"};

    // 随机生成系/部门名称
    private static String[] departments = {"计算机科学", "电子工程", "法学", "数学", "物理学"};

    // 随机生成性别
    private static String[] genders = {"1", "2"};  // 1: 男, 2: 女

    public static void main(String[] args) {
        Random random = new Random();

        // 生成班级数据
        List<String> classIds = generateClassData(random, 10);

        // 生成教师数据
        List<String> teacherIds = generateTeacherData(random, 10);

        // 生成课程数据
        List<String> courseIds = generateCourseData(random, 10);

        // 生成学生数据
        generateStudentData(random, 50, classIds);

        // 生成学生选课数据
        generateScData(random, 50, courseIds);

        // 生成教师课程数据
        generateTcData(random, teacherIds, courseIds);

        // 生成课表数据
        generateSchedulesData(random, classIds, courseIds);
    }

    // 生成班级数据
    private static List<String> generateClassData(Random random, int count) {
        List<String> classIds = new ArrayList<>();
        System.out.println("INSERT INTO class (class_id, class_name) VALUES");
        for (int i = 1; i <= count; i++) {
            String classId = String.format("C%03d", i);
            String className = classNames[random.nextInt(classNames.length)];
            classIds.add(classId);
            System.out.println(String.format("('%s', '%s'),", classId, className));
        }
        return classIds;
    }

    // 生成教师数据
    private static List<String> generateTeacherData(Random random, int count) {
        List<String> teacherIds = new ArrayList<>();
        System.out.println("INSERT INTO teacher (tno, name, sex, age, qualifications, department, password, phone, email) VALUES");
        for (int i = 1; i <= count; i++) {
            String tno = String.format("T%03d", i);
            String name = teacherFirstNames[random.nextInt(teacherFirstNames.length)] + teacherLastNames[random.nextInt(teacherLastNames.length)];
            String sex = genders[random.nextInt(genders.length)];
            int age = random.nextInt(15) + 30;  // 随机30到45岁
            String qualifications = "硕士";
            String department = departments[random.nextInt(departments.length)];
            String password = "password123";
            String phone = "1" + random.nextInt(1000000000, 1999999999);
            String email = tno + "@example.com";
            teacherIds.add(tno);
            System.out.println(String.format("('%s', '%s', %s, %d, '%s', '%s', '%s', '%s', '%s'),",
                    tno, name, sex, age, qualifications, department, password, phone, email));
        }
        return teacherIds;
    }

    // 生成课程数据
    private static List<String> generateCourseData(Random random, int count) {
        List<String> courseIds = new ArrayList<>();
        System.out.println("INSERT INTO course (course_id, tno, cname, credits, hours, department, semester, assess_method) VALUES");
        for (int i = 1; i <= count; i++) {
            String courseId = String.format("C%03d", i);
            String tno = String.format("T%03d", random.nextInt(10) + 1); // 随机选择一个教师
            String cname = courseNames[random.nextInt(courseNames.length)];
            int credits = random.nextInt(2) + 3;  // 随机学分 3或4
            int hours = random.nextInt(30) + 30;  // 随机学时 30到60
            String department = departments[random.nextInt(departments.length)];
            String semester = "2024春季";
            String assessMethod = "期末考试";
            courseIds.add(courseId);
            System.out.println(String.format("('%s', '%s', '%s', %d, %d, '%s', '%s', '%s'),",
                    courseId, tno, cname, credits, hours, department, semester, assessMethod));
        }
        return courseIds;
    }

    // 生成学生数据
    private static void generateStudentData(Random random, int count, List<String> classIds) {
        System.out.println("INSERT INTO student (sno, class_id, name, sex, age, department, password, phone, email) VALUES");
        for (int i = 1; i <= count; i++) {
            String sno = String.format("S%03d", i);
            String classId = classIds.get(random.nextInt(classIds.size()));
            String name = firstNames[random.nextInt(firstNames.length)] + lastNames[random.nextInt(lastNames.length)];
            String sex = genders[random.nextInt(genders.length)];
            int age = random.nextInt(6) + 18;  // 随机18到23岁
            String department = departments[random.nextInt(departments.length)];
            String password = "password123";
            String phone = "1" + random.nextInt(1000000000, 1999999999);
            String email = sno + "@example.com";
            System.out.println(String.format("('%s', '%s', '%s', %s, %d, '%s', '%s', '%s', '%s'),",
                    sno, classId, name, sex, age, department, password, phone, email));
        }
    }

    // 生成学生选课数据
    private static void generateScData(Random random, int studentCount, List<String> courseIds) {
        System.out.println("INSERT INTO score (sno, course_id, score, gpa) VALUES");
        for (int i = 1; i <= studentCount; i++) {
            String sno = String.format("S%03d", i);
            for (int j = 0; j < 3; j++) {  // 每个学生最多选修3门课程
                String courseId = courseIds.get(random.nextInt(courseIds.size()));
                double score = random.nextInt(50) + 50;  // 随机生成成绩
                double gpa = calculateGPA(score);
                System.out.println(String.format("('%s', '%s', %.2f, %.2f),",
                        sno, courseId, score, gpa));
            }
        }
    }

    // 根据成绩计算GPA
    private static double calculateGPA(double score) {
        if (score >= 90) return 4.0;
        else if (score >= 80) return 3.5;
        else if (score >= 70) return 3.0;
        else if (score >= 60) return 2.5;
        else return 2.0;
    }

    // 生成教师-课程数据
    private static void generateTcData(Random random, List<String> teacherIds, List<String> courseIds) {
        System.out.println("INSERT INTO tc (tno, course_id) VALUES");
        for (String teacherId : teacherIds) {
            for (int i = 0; i < 5; i++) {  // 每个教师教授最多5门课程
                String courseId = courseIds.get(random.nextInt(courseIds.size()));
                System.out.println(String.format("('%s', '%s'),", teacherId, courseId));
            }
        }
    }

    // 生成班级-课程安排数据
    private static void generateSchedulesData(Random random, List<String> classIds, List<String> courseIds) {
        System.out.println("INSERT INTO schedules (class_id, course_id, start_time, end_time, location) VALUES");
        String[] locations = {"教学楼1", "教学楼2", "实验楼", "图书馆", "综合楼"};

        for (String classId : classIds) {
            for (String courseId : courseIds) {
                // 随机生成上课时间（只包含小时和分钟）
                int startHour = random.nextInt(8) + 8;  // 随机小时：从8点开始
                int startMinute = random.nextInt(4) * 15;  // 随机分钟：0, 15, 30, 45
                String startTime = String.format("%02d:%02d", startHour, startMinute);  // 格式化时间为 HH:mm

                // 计算结束时间：开始时间 + 2小时 + 15分钟
                int endHour = (startHour + 2) % 24;  // 结束时间小时，加2小时
                int endMinute = (startMinute + 15) % 60;  // 结束时间分钟，加15分钟，超过60会归零

                // 如果分钟数超过60，需要调整小时数
                if (startMinute + 15 >= 60) {
                    endHour = (endHour + 1) % 24;  // 如果分钟数超过60，小时加1
                    endMinute = (startMinute + 15) % 60;  // 确保分钟数在0-59之间
                }

                String endTime = String.format("%02d:%02d", endHour, endMinute);  // 格式化结束时间为 HH:mm

                // 随机选择上课地点
                String location = locations[random.nextInt(locations.length)];

                // 输出 SQL 插入语句
                System.out.println(String.format("('%s', '%s', '%s', '%s', '%s'),", classId, courseId, startTime, endTime, location));
            }
        }
    }




}
